package truedano.truedanostock;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

public class Setting extends Util {
	TextView version_name;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setView();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BREAK){
			closeActivity();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void setView(){
		version_name = (TextView)findViewById(R.id.version_name);
		version_name.setText(getVersionName());
		
		if( getDBSettingCnt("setting00") == 0)
			addDBSetting("setting00");
		
		CheckBox del_small_and_big_cb = (CheckBox)findViewById(R.id.del_small_and_big_cb);
		String DEL_SMALL_AND_BIG = readDBSetting("setting00","DEL_SMALL_AND_BIG");
		if(DEL_SMALL_AND_BIG.equals("0")){
			del_small_and_big_cb.setChecked(false);
		}else{
			del_small_and_big_cb.setChecked(true);
		}
		del_small_and_big_cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					writeDBSetting("setting00", "DEL_SMALL_AND_BIG", "1");
				}else{
					writeDBSetting("setting00", "DEL_SMALL_AND_BIG", "0");
				}
				
			}
		});
		
		final RadioButton rb_5year = (RadioButton)findViewById(R.id.rb_5year);
		final RadioButton rb_7year = (RadioButton)findViewById(R.id.rb_7year);
		rb_5year.setText("5 "+getString(R.string.years));
		rb_7year.setText("7 "+getString(R.string.years));
		String YEAR_TYPE = readDBSetting("setting00","YEAR_TYPE");
		if(YEAR_TYPE.equals("5")){
			rb_5year.setChecked(true);
			rb_7year.setChecked(false);
		}else{
			rb_5year.setChecked(false);
			rb_7year.setChecked(true);
		}
		rb_5year.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					rb_7year.setChecked(false);
					rb_5year.setChecked(true);
					writeDBSetting("setting00", "YEAR_TYPE", "5");
				}
				
			}
		});
		rb_7year.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					rb_7year.setChecked(true);
					rb_5year.setChecked(false);
					writeDBSetting("setting00", "YEAR_TYPE", "7");
				}
				
			}
		});
	}
}
